<template>
  <div class="com-container">
    <div class="com-chart" ref="processprice"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  name: "ProcessPrice",
  data() {
    return {
      mychart: null,
      allData: null, //服务器返回的数据
    };
  },
  created() {
    //在组件创建完成之后进行回调函数的注册
    this.$socket.registerCallBack('processData',this.getData)
  },
  mounted() {
    this.initChart();
    //this.getData();
    //发送数据给服务器，对服务器请求数据
    this.$socket.send({
      action: 'getData',
      socketType: 'processData',
      chartName: 'process_price',
      value: ''
    })
    window.addEventListener("resize", this.screenAdapter);
    //在页面加载完成的时候，主动进行屏幕的适配
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    //在组件销毁之后进行回调函数的取消
    this.$socket.unRegisterCallBack('processData')
  },
  methods: {
    initChart() {
      this.mychart = this.$echarts.init(this.$refs.processprice, this.theme);
      const initOption = {
        title: {
          text: "▎课程价格与平均学习进度关系",
          left: 20,
          top: 20,
        },
        grid: {
          top: "25%",
          left: "3%",
          right: "20%",
          bottom: "3%",
          containLabel: true, //距离是包含坐标轴上的文字
        },
        tooltip: {
          trigger: "axis",
          
        },
        xAxis: {
          type: "category",
          name: "课程价格/元",
        },
        yAxis: {
          type: "value",
          axisLabel: {
            formatter: "{value} %",
          },
        },
        series: [
          {
            type: "line",
            name: '平均学习进度',
            symbol: "triangle",
            symbolSize: 20,
            lineStyle: {
              color: "#87f7cf",
              width: 4,
              type: "dashed",
            },
            itemStyle: {
              borderWidth: 3,
              borderColor: "#EE6666",
              color: "yellow",
            },
            markLine: {
                data: [
                    {type: 'average', name: '平均值'}
                ]
            }
          },
        ],
      };
      this.mychart.setOption(initOption);
    },
    getData(ret) {
      //const { data: ret } = await this.$http.get("process_price");
      console.log(ret, "process_price");
      this.allData = ret;
      this.updateChart();
    },
    updateChart() {
      const xdata = this.allData.map((item) => {
        return item.name;
      });
      const ydata = this.allData.map((item) => {
        return item.value;
      });
      const dataOption = {
        xAxis: { data: xdata },
        series: [{ data: ydata }],
      };
      this.mychart.setOption(dataOption);
    },
    //当浏览器的大小发生变化时，调用该方法，完成屏幕的适配
    screenAdapter() {
      const titleFontSize = (this.$refs.processprice.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: { fontSize: titleFontSize },
        },
      };
      this.mychart.setOption(adapterOption);
      //手动调用图表对象的resize
      this.mychart.resize();
    },
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      this.mychart.dispose()//销毁当前图表
      this.initChart()//重新以最新主题初始化图表对象
      this.screenAdapter()
      this.updateChart()
    }
  },
};
</script>

<style>
</style>